<template>
	<view class="preview">
		<swiper circular :current="currentIndex" @change="swiperChange">
			<swiper-item v-for="(item,index) in classList" :key="item._id">
				<image v-if="readImgs.includes(index)" @click="maskChange" :src="item.picurl" mode="aspectFill"></image>
			</swiper-item>
		</swiper>
		<view class="mask" v-if="maskState">
			<view class="back" @click="goBack" :style="{top:getStatusBarHeight()+'px'}">
				<uni-icons type="back" color="#fff" size="20"></uni-icons>
			</view>
			<view class="count">
				{{currentIndex+1}} / {{classList.length}}
			</view>
			<view class="time">
				<uni-dateformat :date="new Date()" format="hh::mm" />
			</view>
			<view class="date">
				<uni-dateformat :date="new Date()" format="MM月dd日" />
			</view>
			<view class="footer">
				<view class="box" @click="clickInfo">
					<uni-icons type="info" size="28"></uni-icons>
					<view class="text">
						信息
					</view>
				</view>
				<view class="box" @click="clickScore">
					<uni-icons type="star" size="28"></uni-icons>
					<view class="text">
						{{currentInfo.score}}
					</view>
				</view>
				<view class="box" @click="downloadImg">
					<uni-icons type="download" size="23"></uni-icons>
					<view class="text">
						下载
					</view>
				</view>
			</view>
		</view>
		<uni-popup ref="popup" type="bottom">
			<view class="info">
				<view class="header">
					<view>

					</view>
					<view class="title">
						壁纸信息
					</view>
					<view class="close" @click="closeInfo">
						<uni-icons type="closeempty" size="18" color="#999"></uni-icons>
					</view>
				</view>
				<scroll-view scroll-y="true">
					<view class="content">
						<view class="row">
							<view class="label">
								ID:
							</view>
							<text selectable class="value">{{currentInfo._id}}</text>
						</view>
						<!-- 	<view class="row">
							<view class="label">
								分类:
							</view>
							<text selectable class="value">明星美女</text>
						</view> -->
						<view class="row">
							<view class="label">
								发布者:
							</view>
							<text selectable class="value">{{currentInfo.nickname}}</text>
						</view>
						<view class="row">
							<text class="label">评分:</text>
							<view class="value rate">
								<uni-rate :readonly="true" :size="18" :value="currentInfo.score" />
								<view class="score">
									{{currentInfo.score}}分
								</view>
							</view>
						</view>
						<view class="row">
							<view class="label">
								尺寸:
							</view>
							<text selectable class="value">1920x1080</text>
						</view>
						<view class="row">
							<view class="label">
								格式:
							</view>
							<text selectable class="value">jpg</text>
						</view>
						<view class="row">
							<view class="label">
								摘要:
							</view>
							<view class="value">
								{{currentInfo.description}}
							</view>
						</view>
						<view class="row">
							<text class="label">标签:</text>
							<view class="value tabs">
								<view class="tab" v-for="item in currentInfo.tabs">
									{{item}}
								</view>
							</view>
						</view>
						<view class="copyright">
							声明:本站图片资源均收集于互联网，仅供学习研究之用，如侵权请联系删除
						</view>
					</view>
				</scroll-view>
			</view>
		</uni-popup>
		<!-- 评分 -->
		<uni-popup ref="scorePopup" type="center" :is-mask-click="false">
			<view class="scorePopup">
				<view class="header">
					<view>

					</view>
					<view class="title">
						{{isScore?'评分过了':'评分'}}
					</view>
					<view class="close" @click="closeScore">
						<uni-icons type="closeempty" size="18" color="#999"></uni-icons>
					</view>

				</view>
				<view class="content">
					<uni-rate allowHalf v-model='user_score' size="18" :disabled="isScore" />
					<text class="text">{{user_score}}</text>
				</view>
				<view class="footer">
					<button type="default" @click="submitScore" :disabled="!user_score||isScore" size="mini" plain>
						确认评分</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
	import {
		getStatusBarHeight
	} from '@/utils/system.js'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		ref
	} from 'vue';
	import {
		setUpScore
	} from '@/api/api';
	// 点击遮罩层

	const maskState = ref(true)
	const maskChange = () => {
		maskState.value = !maskState.value
	}
	// 点击信息按钮
	const popup = ref(null)
	const clickInfo = () => {
		popup.value.open()
	}
	// 关闭信息弹窗
	const closeInfo = () => {
		popup.value.close()
	}
	const scorePopup = ref(null)
	// 点击评分按钮
	const clickScore = () => {
		if (currentInfo.value.userScore) {
			isScore.value = true
			user_score.value = currentInfo.value.userScore
		}
		scorePopup.value.open()
	}
	// 关闭评分弹窗
	const closeScore = () => {

		scorePopup.value.close()
		isScore.value = false
	}
	const user_score = ref(0)
	const isScore = ref(false)
	// 提交评分
	const submitScore = async () => {

		// 获取壁纸信息
		const {
			classid,
			_id: wallId
		} = currentInfo.value
		const res = await setUpScore({
			classid,
			wallId,
			userScore: user_score.value
		})

		if (res.errCode == 0) {
			uni.showToast({
				title: '评分成功',
				icon: 'success'
			})
			closeScore()
			classList.value[currentIndex.value].userScore = user_score.value
			uni.setStorageSync('storgClassList', classList.value)
		}

	}
	const goBack = () => {
		uni.navigateBack()
	}
	// 获取分类列表
	const currentId = ref(null)
	const currentIndex = ref(0)
	const readImgs = ref([])
	onLoad((e) => {
		currentId.value = e.id
		currentIndex.value = classList.value.findIndex(item => item._id == currentId.value)
		readImags()
		// 获取壁纸信息
		currentInfo.value = classList.value[currentIndex.value]
	})
	const readImags = () => {
		readImgs.value.push(
			// 第一张时
			currentIndex.value <= 0 ? classList.value.length - 1 : currentIndex.value - 1,
			currentIndex.value,
			// 最后一张
			currentIndex.value >= classList.value.length - 1 ? 0 : currentIndex.value + 1
		)
		readImgs.value = [...new Set(readImgs.value)]
	}
	const storgClassList = uni.getStorageSync('storgClassList') || []
	const classList = ref([])
	classList.value = storgClassList.map(item => {
		return {
			...item,
			picurl: item.smallPicurl.replace("_small.webp", ".jpg")
		}
	})
	const swiperChange = (e) => {
		currentIndex.value = e.detail.current
		readImags()
		// 获取壁纸信息
		currentInfo.value = classList.value[currentIndex.value]
	}
	//壁纸信息
	const currentInfo = ref(null)
	// downloadImg
	const downloadImg = () => {
			// #ifdef H5
			uni.showModal({
				content: '请长按保存图片',
				showCancel: false
			})
			// #endif
			// #ifndef H5
			uni.showLoading({
				title: '下载中',
				mask: true
			})
			uni.getImageInfo({
						src: currentInfo.value.picurl,
						success: (res) => {
							uni.saveImageToPhotosAlbum({
								filePath: res.path,
								success: () => {
									uni.showToast({
										title: '保存成功',
										icon: 'success'
									})
								},
								fail: err => {
									if (err.errMsg === 'saveImageToPhotosAlbum:fail cancel') {
										uni.showToast({
											title: '保存失败,请重新点击下载',
											icon: 'error'
										})
										return
									}
									uni.showModal({
										title: "授权提示",
										content: "需要授权保存到相册",
										success: res => {
											if (res.confirm) {
												uni.openSetting({
													success: setting => {
														if (setting.authSetting[
																'scope.writePhoneAlbum'
															]) {
															uni.showToast({
																title: '授权成功',
																icon: 'success'
															})
														} else {
															uni.showToast({
																title: '授权失败',
																icon: 'error'
															})
														}
													}
												})
											}
										}
									})
								},
								complete: () => {
									uni.hideLoading()
								}

							})

							// #endif
						}
</script>

<style lang="scss" scoped>
	.preview {
		width: 100%;
		height: 100vh;
		position: relative;

		swiper {
			width: 100%;
			height: 100%;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.header {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.title {
				color: #333;
				font-size: 26rpx;
			}

			.close {
				padding: 6rpx;
			}
		}

		.info {
			background: #fff;
			padding: 30rpx;
			border-radius: 40rpx 40rpx 0 0;
			overflow: hidden;



			scroll-view {
				height: calc(100vh - 800rpx);
			}

			.content {
				.copyright {
					font-size: 28rpx;
					padding: 20rpx;
					background: #f6f6f6;
					color: #666;
					border-radius: 10rpx;
					margin: 20rpx 0;
					line-height: 1.6em;
				}

				.row {
					display: flex;
					font-size: 32rpx;
					padding: 16rpx 0;
					line-height: 1.7em;

					.label {
						color: #999;
						width: 140rpx;
						text-align: right;
						font-size: 30rpx;
					}

					.value {
						padding-left: 20rpx;
						flex: 1;
						width: 0;
					}

					.rate {
						display: flex;
						align-items: center;

						.score {
							padding-left: 10rpx;
							font-size: 24rpx;
							color: #999;
						}
					}

					.tabs {
						display: flex;
						flex-wrap: wrap;

						.tab {
							border: 1px solid #999;
							color: #777;
							font-size: 22rpx;
							padding: 10rpx 30rpx;
							border-radius: 40rpx;
							line-height: 1em;
							margin: 0 10rpx 10rpx 0;
						}
					}
				}
			}

		}

		.mask {
			&>view {
				position: absolute;
				left: 0;
				right: 0;
				margin: auto;
				color: #fff;
				width: fit-content; // 自适应内容宽度
			}

			.back {
				width: 76rpx;
				height: 76rpx;
				margin: 0;
				background: rgba(0, 0, 0, .5);
				left: 40rpx;
				border-radius: 50%;
				backdrop-filter: blur(10rpx);
				border: 1px solid rgba(255, 255, 255, .3);
				align-items: center;
				justify-content: center;
				display: flex;
			}

			.count {

				top: 10vh;
				background: rgba(0, 0, 0, .3);
				font-size: 28rpx;
				color: #fff;
				padding: 10rpx 20rpx;
				border-radius: 40rpx;
				backdrop-filter: blur(10rpx); // 背景模糊
			}

			.time {

				font-size: 140rpx;
				top: calc(10vh + 80rpx);
				font-weight: 200;
				line-height: 1em;
				text-shadow: 0 4rpx rgba(0, 0, 0, .3);

			}

			.date {
				font-size: 34rpx;
				top: calc(10vh + 230rpx);
				text-shadow: 0 4rpx rgba(0, 0, 0, .3);
			}

			.footer {
				background: rgba(255, 255, 255, .8);
				bottom: 10vh;
				width: 65vw;
				height: 120rpx;
				border-radius: 120rpx;
				color: #000;
				display: flex;
				justify-content: space-around;
				align-items: center;
				backdrop-filter: blur(10rpx); // 背景模糊
				box-shadow: 0 2rpx 0 rgba(0, 0, 0, .1); // 阴影

				.box {
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					padding: 2rpx 12rpx;

					.text {
						font-size: 24rpx;
						color: #777;
						font-weight: 200;
					}
				}


			}


		}

		.scorePopup {
			background: #fff;
			padding: 30rpx;
			width: 70vw;
			border-radius: 30rpx;

			.content {
				padding: 30rpx 0;
				display: flex;
				justify-content: center;
				align-items: center;

				.text {
					color: #ffca3e;
					padding-left: 10rpx;
					width: 80rpx;
					line-height: 1em;
					text-align: right;
				}

			}

			.footer {
				padding: 10rpx 0;
				display: flex;
				justify-content: space-around;
				align-items: center;

				.box {
					padding: 10rpx 20rpx;
					border-radius: 40rpx;
					background: #ffca3e;
					color: #fff;
				}
			}
		}
	}
</style>